#include "multi_button.h"


#define TICKS_INTERVAL    5  //按键状态轮询周期，单位ms
#define DEBOUNCE_TICKS    3  //MAX 7 (0 ~ 7) 去抖时间次数，此为15ms/TICKS_INTERVAL=3次
#define SHORT_TICKS       (300 /TICKS_INTERVAL) //短按时间次数，300ms/TICKS_INTERVAL 
#define LONG_TICKS        (2000 /TICKS_INTERVAL) //长按时间次数，2000ms/TICKS_INTERVAL
         
enum Button_IDs {
  btn1_id,
  btn2_id,
  btn3_id,
};
struct ButtonPara btnpara = {TICKS_INTERVAL, DEBOUNCE_TICKS, SHORT_TICKS, LONG_TICKS};
struct Button btn1;
struct Button btn2;
struct Button btn3;
 
//TODO According to your need to modify the constants.
uint8_t read_button_GPIO(uint8_t button_id)
{
  // you can share the GPIO read function with multiple Buttons
  switch(button_id)
  {
    case btn1_id:
      return GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4);
    case btn2_id:
      return GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3);
    case btn3_id:
      return GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2);
    default:
      return 0;
  }
}
 
void BTN1_PRESS_DOWN_Handler(void* btn)
{
  printf("BTN1_PRESS_DOWN_Handler!\r\n");
}
 
void BTN1_PRESS_UP_Handler(void* btn)
{
  printf("BTN1_PRESS_UP_Handler!\r\n");
}
 
void BTN1_PRESS_REPEAT_Handler(void* btn)
{
  printf("BTN1_PRESS_REPEAT_Handler, repeatcount = %d!\r\n",btn1.repeat);
}
 
void BTN1_SINGLE_Click_Handler(void* btn)
{
  printf("BTN1_SINGLE_Click_Handler!\r\n");
}
 
void BTN1_DOUBLE_Click_Handler(void* btn)
{
  printf("BTN1_DOUBLE_Click_Handler!\r\n");
}
 
void BTN1_LONG_PRESS_START_Handler(void* btn)
{
  printf("BTN1_LONG_PRESS_START_Handler!\r\n");
}
 
void BTN1_LONG_PRESS_HOLD_Handler(void* btn)
{
  printf("BTN1_LONG_PRESS_HOLD_Handler!\r\n");
}
 
void BTN2_SINGLE_Click_Handler(void* btn)
{
  printf("BTN2_SINGLE_Click_Handler!\r\n");
}
 
void BTN2_DOUBLE_Click_Handler(void* btn)
{
  printf("BTN2_DOUBLE_Click_Handler!\r\n");
}
 
void BTN3_LONG_PRESS_START_Handler(void* btn)
{
  printf("BTN3_LONG_PRESS_START_Handler!\r\n");
}
 
void BTN3_LONG_PRESS_HOLD_Handler(void* btn)
{
  printf("BTN3_LONG_PRESS_HOLD_Handler!\r\n");
}
 
int main(void)
{ 
  button_para_init(btnpara);
  button_init(&btn1, read_button_GPIO, 0, btn1_id);
  button_init(&btn2, read_button_GPIO, 0, btn2_id);
  button_init(&btn3, read_button_GPIO, 0, btn3_id);
 
  button_attach(&btn1, PRESS_DOWN,       BTN1_PRESS_DOWN_Handler);
  button_attach(&btn1, PRESS_UP,         BTN1_PRESS_UP_Handler);
  button_attach(&btn1, PRESS_REPEAT,     BTN1_PRESS_REPEAT_Handler);
  button_attach(&btn1, SINGLE_CLICK,     BTN1_SINGLE_Click_Handler);
  button_attach(&btn1, DOUBLE_CLICK,     BTN1_DOUBLE_Click_Handler);
  button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
  button_attach(&btn1, LONG_PRESS_HOLD,  BTN1_LONG_PRESS_HOLD_Handler);
 
  button_attach(&btn2, PRESS_REPEAT,     BTN2_PRESS_REPEAT_Handler);
  button_attach(&btn2, SINGLE_CLICK,     BTN2_SINGLE_Click_Handler);
  button_attach(&btn2, DOUBLE_CLICK,     BTN2_DOUBLE_Click_Handler);
  
  button_attach(&btn3, LONG_PRESS_START, BTN3_LONG_PRESS_START_Handler);
  button_attach(&btn3, LONG_PRESS_HOLD,  BTN3_LONG_PRESS_HOLD_Handler);
 
  button_start(&btn1);
  button_start(&btn2);
  button_start(&btn3);
  
 
  xTaskCreate((TaskFunction_t )key_task,             
                (const char*    )"key_task",           
                (uint16_t       )KEY_STK_SIZE,        
                (void*          )NULL,                  
                (UBaseType_t    )KEY_TASK_PRIO,        
                (TaskHandle_t*  )&KeyTask_Handler);              
    vTaskStartScheduler();          
}
 
 
void key_task(void *pvParameters)
{
  while(1)
  {
    button_ticks();
    vTaskDelay(5);      //5ms周期轮询
  }
}